
### V appendix table A4  - table 2 - all ------------------------------------
do_Ave_Monthly_Spending_All<-  function(data_cost = dt_cost_for_exhibits_all ,
                        data =dt_for_exhibits_all,
                        file_name = "ave_monthly_cost_table2_all_func.tex") { 
  # first version - main cat - planned/ unplanned
  
  dt_cost_all_wide<- dcast.data.table(data_cost,
                                      id_var  +  S_index_date_XX  ~ main_cat ,
                                      value.var = c("actual_cost") ,
                                      fun.aggregate = sum)
  
  top_categories <- names(dt_cost_all_wide[,3:8])
  
  top_cat_table_2_ALL_all <- reweighting_categories_all(dt_cost_all_wide,
                                                        data,
                                                        top_categories,
                                                        31,0.1)
  
  # All - Second version - main cat - Intensity 
  dt_cost_all_wide_Intensity<- dcast.data.table(data_cost[
    ][,new_cat := main_cat][
      main_cat%in%c("Inpatient_Unplanned",
                    "Inpatient_Planned"),
      new_cat:=factor(ifelse(profession%in%intensity_wards,
                             "Low","High")) ],
    id_var  +  S_index_date_XX  ~ new_cat ,
    value.var = c("actual_cost") ,
    fun.aggregate = sum)
  
  top_categories_inten<- names(dt_cost_all_wide_Intensity[,3:8])
  
  top_cat_table_2_ALL_Intensity_all <- reweighting_categories_all(dt_cost_all_wide_Intensity,
                                                                  data,
                                                                  top_categories_inten,
                                                                  31,0.1)
  rrr<-
    combine_table_2(top_cat_table_2_ALL_Intensity_all,
                    top_cat_table_2_ALL_all,
                   file_name)
  
  return(rrr)
}
